<template>
    <common-layout>
        <div class="top">
            <div class="header">
                <img alt="logo" class="logo" src="@/assets/img/logo.png"/>
                <span class="title">{{ systemName }}</span>
            </div>
            <div class="desc">规则引擎前端，业务逻辑实现不再依赖于代码开发，可零代码实现复杂业务逻辑。</div>
        </div>
        <div class="login">
            <a-form @submit="onSubmit" :form="form">
                <a-tabs size="large" :tabBarStyle="{textAlign: 'center'}" style="padding: 0 2px;">
                    <a-tab-pane tab="系统登陆" key="1">
                        <a-alert type="error" :closable="true" v-show="error" :message="error" showIcon
                                 style="margin-bottom: 24px;"/>
                        <a-form-item>
                            <a-input
                                    autocomplete="autocomplete"
                                    size="large"
                                    placeholder="请输入用户名"
                                    v-decorator="['username', {initialValue:'lq',rules: [{ required: true, message: '请输入账户名', whitespace: true}]}]"
                            >
                                <a-icon slot="prefix" type="user"/>
                            </a-input>
                        </a-form-item>
                        <a-form-item>
                            <a-input
                                    size="large"
                                    placeholder="请输入密码"
                                    autocomplete="autocomplete"
                                    type="password"
                                    v-decorator="['password', {initialValue:'123',rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
                            >
                                <a-icon slot="prefix" type="lock"/>
                            </a-input>
                        </a-form-item>
                    </a-tab-pane>
                </a-tabs>
                <div>
                    <a-checkbox :checked="true">自动登录</a-checkbox>
                    <a style="float: right">忘记密码</a>
                </div>
                <a-form-item>
                    <a-button :loading="logging" style="width: 100%;margin-top: 24px" size="large" htmlType="submit"
                              type="primary">登录
                    </a-button>
                </a-form-item>
                <div>
                    其他登录方式
                    <a-icon class="icon" type="alipay-circle"/>
                    <a-icon class="icon" type="taobao-circle"/>
                    <a-icon class="icon" type="weibo-circle"/>
                </div>
            </a-form>
        </div>
    </common-layout>
</template>

<script>
    import CommonLayout from '@/layouts/CommonLayout'
    import {getRoutesConfig, login} from '@/services/user'
    import {loadRoutes} from '@/utils/routerUtil'
    import {mapMutations} from 'vuex'

    export default {
        name: 'Login',
        components: {CommonLayout},
        data() {
            return {
                logging: false,
                error: '',
                form: this.$form.createForm(this)
            }
        },
        computed: {
            systemName() {
                return this.$store.state.setting.systemName
            }
        },
        methods: {
            ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),
            onSubmit(e) {
                e.preventDefault()
                this.form.validateFields((err) => {
                    if (!err) {
                        this.logging = true
                        const username = this.form.getFieldValue('username')
                        const password = this.form.getFieldValue('password')
                        login(username, password).then(this.afterLogin)
                    }
                })
            },
            afterLogin(res) {
                this.logging = false
                const loginRes = res.data
                if (loginRes.code === 200) {
                    this.$message.success('登陆成功', 3)
                    // 获取路由配置
                    getRoutesConfig().then(result => {
                        const routesConfig = result.data.data
                        loadRoutes(routesConfig)
                        this.$router.push('/dashboard/workplace')
                    })
                } else {
                    this.error = loginRes.message

                }
            }
        }
    }
</script>

<style lang="less" scoped>
    .common-layout {
        .top {
            text-align: center;

            .header {
                height: 44px;
                line-height: 44px;

                a {
                    text-decoration: none;
                }

                .logo {
                    height: 44px;
                    vertical-align: top;
                    margin-right: 16px;
                }

                .title {
                    font-size: 33px;
                    color: @title-color;
                    font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
                    font-weight: 600;
                    position: relative;
                    top: 2px;
                }
            }

            .desc {
                font-size: 14px;
                color: @text-color-second;
                margin-top: 12px;
                margin-bottom: 40px;
            }
        }

        .login {
            width: 368px;
            margin: 0 auto;
            @media screen and (max-width: 576px) {
                width: 95%;
            }
            @media screen and (max-width: 320px) {
                .captcha-button {
                    font-size: 14px;
                }
            }

            .icon {
                font-size: 24px;
                color: @text-color-second;
                margin-left: 16px;
                vertical-align: middle;
                cursor: pointer;
                transition: color 0.3s;

                &:hover {
                    color: @primary-color;
                }
            }
        }
    }
</style>
